package com.shixu.framework.plugins.weixin.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.shixu.framework.plugins.weixin.bean.Article;
import com.shixu.framework.plugins.weixin.util.HttpKit;


/**
 * 图文消息素材
 * @author luy
 *
 */
public class ArticleService {

	// 上传图文消息素材
	public final String ARTICLE_UPLOADNEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=";
	
	/**
	 * 上传图文消息素材
	 * @param access_token 在微信平台获取到的凭证
	 * @param articles 图文消息素材的集合
	 * @return
	 * @throws IOException
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public JSONObject uploadnews(String access_token,List<Article> articles) throws IOException, ExecutionException, InterruptedException{
		String result = null;
		
		JSONArray articlesArray = new JSONArray();
		if (articles!=null && articles.size()>0) {
			for (Article item : articles) {
				articlesArray.add(item.getData());
			}
		}
		
		JSONObject articlesJson = new JSONObject();
		articlesJson.put("articles", articlesArray);
		  
		result = HttpKit.post(ARTICLE_UPLOADNEWS_URL + access_token, articlesJson.toString());
		
		if(StringUtils.isNotEmpty(result)){
    		return JSONObject.fromObject(result);
    	}
        return null;
	}
	  

	/**
	 * 测试上传图文消息素材
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException { 
		String result = null;
		
		List<Article> articles = new ArrayList<Article>();
		
		Article article = new Article();
		article.setThumb_media_id("IVMlpMM6YDprYIzyQ-msWFtVKV8LCEuim9jzIVHaB4-yH2Bweqm14jQrO3wfIi5v");
		article.setAuthor("张三");
		article.setTitle("测试上传图文消息素材1");
		article.setContent_source_url("www.baidu.com");
		article.setContent("测试内容");
		article.setDigest("图文消息描述");
		article.setShow_cover_pic("1");
		articles.add(article);
		
		Article article2 = new Article();
		article2.setThumb_media_id("IVMlpMM6YDprYIzyQ-msWFtVKV8LCEuim9jzIVHaB4-yH2Bweqm14jQrO3wfIi5v");
		article2.setAuthor("栗色");
		article2.setTitle("测试上传图文消息素材1");
		article2.setContent_source_url("www.baidu.com");
		article2.setContent("测试内容1");
		article2.setDigest("图文消息描述1");
		article2.setShow_cover_pic("0");
		articles.add(article2);
		
		
		ArticleService articleService = new ArticleService();
		try {
			String access_token = "fWbIL3Gy1pPkF9aNIXWzV9CreOH9wxV8GELTchOBvDvFv2jFYB9RyKmMr9phXYSYYctufMK6n3IahwRKVC7h0FjZqlKxH0A_nEo8HI-6nSU";
			result = articleService.uploadnews(access_token, articles).toString();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	    System.out.println(result);  
	  
	}  

}
